//
//  FSCellPhoneController.m
//  WZSOA
//
//  Created by 刘沿 on 16/7/18.
//  Copyright © 2016年 刘沿. All rights reserved.
//

#import "FSCellPhoneController.h"

@interface FSCellPhoneController ()

@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation FSCellPhoneController

static NSString *const reuserID = @"cellPhone";

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }

    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"通讯录";
    
    // 添加一个头部的刷新控件
    __weak typeof (self)WeakSelf = self;
    
    [self.tableView addHeaderWithCallback:^{
        
        [WeakSelf loadData];
        
        
    }];
    
    //主动让下拉刷新头部控件进入刷新状态
    [self.tableView headerBeginRefreshing];
    
    //    添加一个尾部的刷新控件
    
//    [self.tableView addFooterWithCallback:^{
//        
//        if ((self.currentPage - 1) * KPageSize < self.totalCounts) {
//            
//            [weakSelf loadMore];
//        }else {
//            
//            [weakSelf.tableView footerEndRefreshing];
//            [weakSelf.view makeToast:@"没有更多了"];
//        }
//        
//    }];
}

- (void)loadData{

    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];

    NSArray *array = @[@{@"userId":userId}];
    
    [HttpTools requestWithURL:Kexchother arrays:array method:KglobalTxlTree success:^(id JSON) {
        
        NSArray *temptArray = JSON[@"data"];
        
        NSDictionary *dict1 = temptArray.firstObject;
        
        for (NSDictionary *dict in dict1[@"Parent"]) {
            
            FSCellPhoneModel *model = [[FSCellPhoneModel alloc] init];
            
            model.isthree = dict[@"isthree"];
            
            model.name = dict[@"name"];
            
            model.unid = dict[@"unid"];
            
            [self.dataArray addObject:model];
        }
        
        [self.tableView headerEndRefreshing];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuserID];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserID];
    }
    
    FSCellPhoneModel *model = self.dataArray[indexPath.row];
    
    cell.textLabel.text = model.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    FSCellPhoneModel *model = self.dataArray[indexPath.row];

    FSSecondCellPhoneController *secCellVC = [[FSSecondCellPhoneController alloc] init];
    
    secCellVC.isThree = model.isthree;
    
    secCellVC.unid = model.unid;

    [self.navigationController pushViewController:secCellVC animated:YES];
    
}


@end









